import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.event.MouseWheelEvent;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ling
 * Date: 2022-10-16
 * Time: 17:09
 */
@WebServlet("/message")
public class MessageServlet extends HttpServlet {
    //由于ObjectMapper 会在多个方法中使用，就提出来，作为成员变量
    private ObjectMapper objectMapper =new ObjectMapper();
    //private List<Message> messageList = new ArrayList<>();
    //负责让实现让客户端提交数据给服务器
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Message message = objectMapper.readValue(req.getInputStream(),Message.class);
        //messageList.add(message);
        save(message);
        System.out.println("message"+message);
        resp.setContentType("application/json;charset=utf8");
        resp.getWriter().write("{\"ok\": 1}");
    }
    //负责实现让客户端从服务器拿到数据
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 由于约定的请求, 没有参数, 不需要进行任何解析操作~~
        resp.setContentType("application/json;charset=utf8");
        // 把对象转成 json 格式的字符串. 此处 messageList 是一个 List, 直接就被转成 json 数组了.
        List<Message> messageList = load();
        String respString = objectMapper.writeValueAsString(messageList);
        resp.getWriter().write(respString);
    }
    //把当前的消息存到数据库中
    private void save(Message message)  {
        Connection connection=null;
        PreparedStatement statement=null;
        //1.和数据库建立连接
        try {
            connection = DBUtil.getConnection();
            //2.构造sql语句
            String sql="insert into message value (?,?,?);";
            statement = connection.prepareStatement(sql);
            statement.setString(1,message.from);
            statement.setString(2,message.to);
            statement.setString(3,message.message);
            //3.执行SQL语句
            int ret = statement.executeUpdate();
            if(ret!=1) {
                System.out.println("插入失败");
            } else {
                System.out.println("插入成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //4.关闭连接
            DBUtil.close(connection,statement,null);
        }

    }
    //从数据库查询到的记录
    private List<Message> load() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Message> messageList = new ArrayList<>();
        try {
            //1.建立连接
            connection=DBUtil.getConnection();
            //2.构造sql语句
            String sql="select * from message";
            statement=connection.prepareStatement(sql);
            //3.执行语句
            resultSet=statement.executeQuery();
            //将结果接存入messageList
            while(resultSet.next()) {
                Message message=new Message();
                message.from=resultSet.getString("from");
                message.to=resultSet.getString("to");
                message.message=resultSet.getString("message");
                messageList.add(message);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return messageList;
    }
}
class Message {
    public String from;
    public String to;
    public String message;

    @Override
    public String toString() {
        return "Message{" +
                "from='" + from + '\'' +
                ", to='" + to + '\'' +
                ", message='" + message + '\'' +
                '}';
    }
}
